package team.jaman.autosell.controller;

import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import team.jaman.autosell.entity.Logger;
import team.jaman.autosell.entity.Manager;
import team.jaman.autosell.service.ManagerService;
import team.jaman.autosell.tools.CommonData;
import team.jaman.autosell.tools.KeptchaData;
import team.jaman.autosell.tools.PageData;

@Controller
@RequestMapping("manager")
public class ManagerController {
	@Resource
	ManagerService managerService;
	
	// log4j日志
	private org.apache.log4j.Logger log;

	/**
	 * 根据账号密码查询管理员
	 */
	@RequestMapping(value = "login", method = RequestMethod.POST)
	@ResponseBody
	public KeptchaData loginManager(@RequestBody KeptchaData keptchaData, HttpSession session) {	
		//log.info(keptchaData.getManagerName());
		//log.info(keptchaData.getVerity());
		Logger logger = new Logger();
		logger.setStartTime(new Date());
		
		// session中获取的验证码值
		String keptcha = (String) session.getAttribute(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);
		// 校验验证码
		if(keptchaData.getVerity() == null || !keptchaData.getVerity().equalsIgnoreCase(keptcha)) {
			keptchaData.setMessage("验证码错误");
		} else {
			
			Manager manager = managerService.loginManager(keptchaData.getManager(),logger);
			//log.info(newManager);
			if(manager == null) {
				keptchaData.setMessage("账号或密码错误");
			} else {
				keptchaData.setMessage("登录成功");
				session.setAttribute("manager", manager);
			}			
		}
		return keptchaData;
	}

	/**
	 * 添加管理员
	 */
	@RequestMapping(value = "add", method = RequestMethod.POST)
	public CommonData addManager(@RequestBody Manager record) {
		Logger logger = new Logger();
		logger.setStartTime(new Date());
		CommonData commonData = managerService.addManager(record,logger);
		System.out.println(commonData.getMsg());

		return commonData;
	}

	/**
	 * 修改管理员
	 */
	@RequestMapping(value = "update", method = RequestMethod.PUT, produces = "application/json;charset=utf-8")
	@ResponseBody
	public CommonData updateManager(@RequestBody Manager record,HttpSession session) {
		Logger logger = new Logger();
		logger.setStartTime(new Date());

		CommonData commonData = managerService.updateByPrimaryKeySelective(record,logger);
		
		session.removeAttribute("manager");
		Manager manager = managerService.selectManagerById(record.getManagerId());
		session.setAttribute("manager", manager);

		// indexManager 管理员首页； addManager 添加管理员页面资源
		return commonData;
	}

	/**
	 * 查询管理员
	 */
	@ResponseBody
	@RequestMapping(value = "selectLike", method = RequestMethod.GET, produces = "application/json;charset=utf-8")
	public PageData<Manager> selectManager(@RequestParam(value = "page", defaultValue = "1") int page,@RequestParam(value = "limit", defaultValue = "10")  int limit,@RequestParam(value = "keyword", defaultValue = "") String keyword) {
		
		System.out.println("keyword:" + keyword);
		PageData<Manager> pageData = managerService.selectManager(page, limit, keyword);
		pageData.getData().forEach(manager -> System.out.println(manager));

		return pageData;
	}

}
